package utils
{
	import flash.utils.Dictionary;
	
	public class HashMap
	{
		/**
		 *数据集合 
		 */		
		private var _dictionary:Dictionary;
		/**
		 *数据集合 
		 * @return 
		 */
		public function get dictionary():Dictionary
		{
			return _dictionary;
		}

		/**
		 *key集合列表 
		 */		
		private var _keys:Array;
		
		public function HashMap()
		{
			_dictionary=new Dictionary();
			_keys=[];
		}
		
		/**
		 * 加入元素至尾端 
		 * @param key 键
		 * @param value 值
		 * @return 当前的长度
		 */		
		public function push(key:Object, value:Object):uint
		{
			if(key==null)
			{
				throw new ArgumentError("往HashMap中push数据时，key为空。");
				return;
			}
			_keys.push(key);
			_dictionary[key]=value;
			return _keys.push(key)
		}
		
		/**
		 * 加入元素至首端 
		 * @param key 键
		 * @param value 值
		 * @return 当前的长度
		 */	
		public function unshift(key:Object, value:Object):uint
		{
			if(key==null)
			{
				throw new ArgumentError("往HashMap中unshift数据时，key为空。");
				return;
			}
			_dictionary[key]=value;
			return _keys.unshift(key);
		}
		
		/**
		 *根据键取得值 
		 * @param key 键
		 * @return 返回取得的值
		 */		
		public function get(key:Object):Object
		{
			return _dictionary[key];
		}
		
		/**
		 *根据键移除 
		 * @param key 键
		 * @return 移除后的值
		 */		
		public function remove(key:Object):Object
		{
			var index:int=_keys.indexOf(key);
			_keys.splice(index, 1);
			var value:Object=_dictionary[key];
			if(value!=null)
			{
				delete _dictionary[key];
				return value;
			}
			return null;
		}
		
		/**
		 * 是否包含指定的键
		 * @param key 键
		 * @return 包含返回true,否则返回false
		 */		
		public function containsKey(key:Object):Boolean
		{
			return _keys.indexOf(key)!=-1;
		}
		
		/**
		 * 是否包含指定的值
		 * @param value
		 * @return 包含返回true,否则返回false
		 */		
		public function containsValue(value:Object):Boolean
		{
			for each(var v:Object in _dictionary)
			{
				if(v === value)
				{
					return true;
				}
			}
			return false;
		}
		
		/**
		 *长度 
		 * @return 
		 */		
		public function get length():int
		{
			return _keys.length;
		}
		
		/**
		 *是否为空 
		 * @return 无任何数据返回true,否则返回false
		 */		
		public function isEmpty():Boolean
		{
			return _keys.length == 0;
		}
		
		/**
		 *清除
		 */		
		public function clear():void
		{
			_keys.length=0;
			for(var key:Object in _dictionary)
			{
				delete _dictionary[key];
			}
		}
	}
}